const http = axios.create({
  baseURL: "http://localhost:8080/api",
  timeout: 10000,
});

http.interceptors.request.use(
  (config) => {
    const token = getToken();
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

http.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// ---------------------- Menu -----------

function listMenu() {
  return http.get("/menu/list");
}

// ------------------------ User-----------------------------
function login(body) {
  return http.post("/user/login", body);
}
function register(body) {
  return http.post("/user/register", body);
}

function addUser(body) {
  return http.post("/user/add", body);
}

function deleteUser(id) {
  return http.delete(`/user/remove/${id}`);
}

function updateUser(body) {
  return http.post("/user/update", body);
}

function getUser() {
  return http.get("/user/get");
}

function listUser(body, { pageNum, pageSize }) {
  return http.post(`/user/list?pageNum=${pageNum}&pageSize=${pageSize}`, body);
}

// ------------------------ Position-----------------------------
function listPosition() {
  return http.get("/position/list");
}

// ------------------------ Role-----------------------------
function addRole(body) {
  return http.post("/role", body);
}

function deleteRole(id) {
  return http.delete(`/role/${id}`);
}

function updateRole(body) {
  return http.put("/role", body);
}

function getRole(id) {
  return http.get(`/role/${id}`);
}
function listRole() {
  return http.get("/role/list");
}

function pageRole(body, { pageNum, pageSize }) {
  return http.post(`/role/page?pageNum=${pageNum}&pageSize=${pageSize}`, body);
}

// ------------------------ Team-----------------------------
function addTeam(body) {
  return http.post("/team", body);
}

function deleteTeam(id) {
  return http.delete(`/team/${id}`);
}

function updateTeam(body) {
  return http.put("/team", body);
}

function getTeam(id) {
  return http.get(`/team/${id}`);
}
function listTeam() {
  return http.get("/team/list");
}

function pageTeam(body, { pageNum, pageSize }) {
  return http.post(`/team/page?pageNum=${pageNum}&pageSize=${pageSize}`, body);
}

// ------------------------ Place-----------------------------
function addPlace(body) {
  return http.post("/place", body);
}

function deletePlace(id) {
  return http.delete(`/place/${id}`);
}

function updatePlace(body) {
  return http.put("/place", body);
}

function getPlace(id) {
  return http.get(`/place/${id}`);
}
function listPlace() {
  return http.get("/place/list");
}

function pagePlace(body, { pageNum, pageSize }) {
  return http.post(`/place/page?pageNum=${pageNum}&pageSize=${pageSize}`, body);
}

// ------------------------ Season-----------------------------
function addSeason(body) {
  return http.post("/season", body);
}

function deleteSeason(id) {
  return http.delete(`/season/${id}`);
}

function updateSeason(body) {
  return http.put("/season", body);
}

function getSeason(id) {
  return http.get(`/season/${id}`);
}
function listSeason() {
  return http.get("/season/list");
}

function pageSeason(body, { pageNum, pageSize }) {
  return http.post(
    `/season/page?pageNum=${pageNum}&pageSize=${pageSize}`,
    body
  );
}

// ------------------------ Skill-----------------------------
function addSkill(body) {
  return http.post("/skill", body);
}

function deleteSkill(id) {
  return http.delete(`/skill/${id}`);
}

function updateSkill(body) {
  return http.put("/skill", body);
}

function getSkill(id) {
  return http.get(`/skill/${id}`);
}
function listSkill() {
  return http.get("/skill/list");
}

function pageSkill(body, { pageNum, pageSize }) {
  return http.post(`/skill/page?pageNum=${pageNum}&pageSize=${pageSize}`, body);
}

// ------------------------ AgeGroup-----------------------------
function addAgeGroup(body) {
  return http.post("/ageGroup", body);
}

function deleteAgeGroup(id) {
  return http.delete(`/ageGroup/${id}`);
}

function updateAgeGroup(body) {
  return http.put("/ageGroup", body);
}

function getAgeGroup(id) {
  return http.get(`/ageGroup/${id}`);
}
function listAgeGroup() {
  return http.get("/ageGroup/list");
}

function pageAgeGroup(body, { pageNum, pageSize }) {
  return http.post(
    `/ageGroup/page?pageNum=${pageNum}&pageSize=${pageSize}`,
    body
  );
}

// ------------------------ Training-----------------------------
function addTraining(body) {
  return http.post("/training", body);
}

function deleteTraining(id) {
  return http.delete(`/training/${id}`);
}

function updateTraining(body) {
  return http.put("/training", body);
}

function getTraining(id) {
  return http.get(`/training/${id}`);
}
function listTraining() {
  return http.get("/training/list");
}

function pageTraining(body, { pageNum, pageSize }) {
  return http.post(
    `/training/page?pageNum=${pageNum}&pageSize=${pageSize}`,
    body
  );
}

// ------------------------ Match-----------------------------
function addMatch(body) {
  return http.post("/match", body);
}

function deleteMatch(id) {
  return http.delete(`/match/${id}`);
}

function updateMatch(body) {
  return http.put("/match", body);
}

function getMatch(id) {
  return http.get(`/match/${id}`);
}
function listMatch() {
  return http.get("/match/list");
}

function pageMatch(body, { pageNum, pageSize }) {
  return http.post(`/match/page?pageNum=${pageNum}&pageSize=${pageSize}`, body);
}
